{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为___None____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为___[30,4]____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为___5____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为___{97}____."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(  F  )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(  T  )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(  T  )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(  F  )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:[1,2,2,3]\n"
     ]
    }
   ],
   "source": [
    "x = eval(input('请输入一个列表:'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({2: 2, 1: 1, 3: 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "frequences = Counter(x)\n",
    "print(frequences)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:'red,yellow,orange,green,orange'\n"
     ]
    }
   ],
   "source": [
    "s = eval(input('请输入一个字符串:'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "print(s.find('red'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(s.find('yellow'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "print(s.find('green'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:[1,2,3,2,1]\n"
     ]
    }
   ],
   "source": [
    "y = eval(input('请输入一个列表:'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(y.count(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(y.count(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(y.count(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入若干集合的列表:[{1,2,3},{2,4,3},{4,2,1}]\n"
     ]
    }
   ],
   "source": [
    "z = eval(input('请输入若干集合的列表:'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "print(reduce(lambda x,y:x|y,z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:'red,yellow,orange,green,orange'\n"
     ]
    }
   ],
   "source": [
    "str1 = input('请输入一个字符串:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No\n"
     ]
    }
   ],
   "source": [
    "str2 = str1[::3]\n",
    "if str1 ==str2:\n",
    "    print('Yes')\n",
    "else:\n",
    "    print('No')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1.可迭代对象\n",
    "2.False\n",
    "3.None\n",
    "4.remove()\n",
    "5.[6,7,9,11]\n",
    "6.大括号；“键”；“值”；“键”\n",
    "7.items();keys();values()\n",
    "8.c = dict(zip(a,b))\n",
    "9.b = a[::3]\n",
    "10.[5 for i in range(10)]\n",
    "11.不可以\n",
    "12.\n",
    "13.\n",
    "14.列表的增删会产生内存扩展和缩进，从尾部进行删除不会影响前面的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 2\n",
      "1 : 1\n",
      "2 : 2\n",
      "3 : 2\n",
      "5 : 2\n",
      "6 : 1\n",
      "9 : 1\n",
      "11 : 1\n",
      "12 : 2\n",
      "13 : 1\n",
      "14 : 1\n",
      "15 : 1\n",
      "16 : 2\n",
      "17 : 4\n",
      "18 : 1\n",
      "19 : 1\n",
      "21 : 1\n",
      "22 : 4\n",
      "23 : 2\n",
      "24 : 1\n",
      "26 : 1\n",
      "29 : 1\n",
      "30 : 2\n",
      "31 : 1\n",
      "32 : 1\n",
      "33 : 1\n",
      "35 : 2\n",
      "36 : 1\n",
      "38 : 1\n",
      "42 : 2\n",
      "44 : 1\n",
      "46 : 3\n",
      "47 : 1\n",
      "48 : 2\n",
      "49 : 1\n",
      "51 : 1\n",
      "52 : 1\n",
      "54 : 2\n",
      "55 : 2\n",
      "56 : 4\n",
      "58 : 2\n",
      "59 : 1\n",
      "60 : 1\n",
      "61 : 2\n",
      "62 : 2\n",
      "64 : 1\n",
      "65 : 1\n",
      "67 : 2\n",
      "69 : 1\n",
      "72 : 1\n",
      "74 : 1\n",
      "75 : 2\n",
      "78 : 2\n",
      "79 : 1\n",
      "80 : 1\n",
      "81 : 1\n",
      "84 : 1\n",
      "87 : 2\n",
      "88 : 2\n",
      "90 : 1\n",
      "91 : 2\n",
      "92 : 1\n",
      "93 : 1\n",
      "94 : 1\n",
      "97 : 1\n",
      "99 : 2\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "r = [random.randint(0,100)for i in range(100)]\n",
    "d = set(r)\n",
    "for v in d:\n",
    "    print(v,':',r.count(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please input a list:[1,2,3,4,5,6]\n",
      "Please input the start position and the end position:0,3\n",
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "g = input('Please input a list:')\n",
    "\n",
    "g = eval(g)\n",
    "\n",
    "start,end = eval(input('Please input the start position and the end position:'))\n",
    "\n",
    "print(g[start:end])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[71, 31, 78, 52, 58, 17, 33, 12, 7, 32, 4, 0, 94, 75, 58, 38, 40, 71, 22, 40]\n",
      "[7, 12, 17, 31, 32, 33, 52, 58, 71, 78, 94, 75, 71, 58, 40, 40, 38, 22, 4, 0]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "u = [random.randint(0,100)for i in range(20)]\n",
    "\n",
    "print(u)\n",
    "\n",
    "p = u[0:10]\n",
    "\n",
    "p.sort()\n",
    "\n",
    "u[0:10] = p\n",
    "\n",
    "p = u[10:20]\n",
    "\n",
    "p.sort(reverse=True)\n",
    "\n",
    "u[10:20] = p\n",
    "\n",
    "print(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
